package com.work.shart.web.controller.classifcation;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.work.shart.common.mybatis.service.impl.SuperServiceImpl;
import com.work.shart.common.result.ResultVo;
import com.work.shart.web.dto.classifcation.ClassifcationUpdateDTO;
import com.work.shart.web.entity.classifcation.ClassificationEntity;
import com.work.shart.web.res.classifcation.ClassifcationParentRes;
import com.work.shart.web.service.classifcation.IClassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.work.shart.common.result.Result.success;

@RestController
@RequestMapping("/api/classifcation")
@Slf4j
@Api(tags = "分类")
public class ClassifcationController extends SuperServiceImpl<IClassificationService, ClassificationEntity> {

    @ApiOperation(value = "热点分类")
    @GetMapping("hotList")
    public ResultVo<List<ClassificationEntity>> hotList() {
        QueryWrapper<ClassificationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("click_num", 20);
        queryWrapper.notIn("parent_id", 0);
        return success(getBaseService().list(queryWrapper));
    }

    /**
     * 分类
     *
     * @return
     */
    @ApiOperation(value = "侧边栏分类带子集")
    @GetMapping("parentList")
    public ResultVo<List<ClassifcationParentRes>> prentList() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.orderByAsc("sort");
        List<ClassificationEntity> classificationEntityList = getBaseService().list(queryWrapper);
        List<ClassifcationParentRes> res = Lists.newArrayList();
        classificationEntityList.forEach(classificationEntity -> {
            QueryWrapper objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("parent_id", classificationEntity.getId());
            objectQueryWrapper.orderByAsc("sort");
            List<ClassificationEntity> classificationEntities = Lists.newArrayList();
            classificationEntities.addAll(getBaseService().list(objectQueryWrapper));
            ClassifcationParentRes classifcationParentRes = ClassifcationParentRes.builder()
                    .parent(classificationEntity.getId())
                    .parentName(classificationEntity.getName())
                    .list(classificationEntities)
                    .build();
            res.add(classifcationParentRes);
        });
        return success(res);
    }

    @ApiOperation(value = "修改为热点分类")
    @PostMapping("updateHotSpot")
    public ResultVo updateHotSpot(@Validated @RequestBody ClassifcationUpdateDTO classifcationUpdateDTO) {
        ClassificationEntity classificationEntity = getBaseService().getById(classifcationUpdateDTO.getId());
        classificationEntity.setClickNum(classificationEntity.getClickNum() + 1);
        getBaseService().updateById(classificationEntity);
        if (classificationEntity.getClickNum() > 20)
            getBaseService().updateById(BeanUtil.copyProperties(classifcationUpdateDTO, getEntityClass()));
        return success();
    }

}
